package java学习.Test;

import java.util.*;

/**
 * **************
 * 项目名称: 蓝桥杯-Java学习 <br/>
 * 文件名称:  <br/>
 * 文件描述: 这里添加您的类文件描述，说明当前文件要包含的功能。 <br/>
 * 文件创建：刘世锦Lenovo <br/>
 * 创建时间: 2023/2/13 <br/>
 *
 * @version v1.0 <br/>
 * @update [序号][日期YYYY-MM-DD][更改人姓名][变更描述]<br/>
 * *************
 */
public class testDealMap {

    public static void main(String[] args) {
        List<Map<String, Object>> queriedData = new ArrayList<>();
        Map<String, Object> result = new HashMap<>();
        result.put("appraiserType", "其他部门正副职");
        result.put("appraiserAccount", "duj223");
        result.put("appraiser", "杜鹃");
        result.put("demention", "工作绩效");
        result.put("dementionScore", "38");
        result.put("appraiserScore", "50.3");
        result.put(result.get("demention")+"", result.get("dementionScore"));
        queriedData.add(result);
        Map<String, Object> result1 = new HashMap<>();
        result1.put("appraiserType", "其他部门正副职");
        result1.put("appraiserAccount", "duj223");
        result1.put("appraiser", "杜鹃");
        result1.put("demention", "工作能力");
        result1.put("dementionScore", "16");
        result1.put("appraiserScore", "50.3");
        result1.put(result1.get("demention")+"", result1.get("dementionScore"));
        queriedData.add(result1);
        Map<String, Object> result2 = new HashMap<>();
        result2.put("appraiserType", "其他部门正副职");
        result2.put("appraiserAccount", "duj223");
        result2.put("appraiser", "杜鹃");
        result2.put("demention", "工作素质");
        result2.put("dementionScore", "6");
        result2.put("appraiserScore", "50.3");
        result2.put(result2.get("demention")+"", result2.get("dementionScore"));
        queriedData.add(result2);
        System.out.println(queriedData);
//		for (int i = 0 ; i < queriedData.size() ;i++  ) {
//			for (int j = 0; j < queriedData.size(); j++) {
//
//				Map<String, Object> queriedMap = queriedData.get(i);
//				Map<String, Object> mapJ = queriedData.get(j);
//				String accounti = (String) queriedMap.get("appraiserAccount");
//				String accountj = (String) mapJ.get("appraiserAccount");
////                        queriedMap.put("" + queriedMap.get("demention"), queriedMap.get("dementionScore"));
//				if (accountj.equals(accounti)) {
//					queriedMap.put(mapJ.get("demention") + "", mapJ.get("dementionScore"));
////					queriedData.remove(j);
//				}
//			}
////                    queriedMap.put((String) queriedMap.get("demention"),queriedMap.get("dementionScore"));
//		}
//		System.out.println(removeRepeatMapByKey(queriedData,"appraiser"));
//		System.out.println(removeRepeatMapByKey1(queriedData,"appraiser"));
        List<Map<String,Map<String, Object>>> resultResult = new ArrayList<>();
        Map<String, Map<String,Object>> resultMap1 = new HashMap<>();
        Map<String, Map<String, Object>> newMap = new HashMap<>();
        int index = 1;
//        for (Map<String, Object> itemMap : queriedData) {
//            String userAccount = itemMap.get("appraiserAccount").toString();
//            int totalScore = Integer.parseInt(itemMap.get("dementionScore").toString());
//            if (newMap.containsKey(userAccount)) {
//                Map<String, Object> userInfoMap = newMap.get(userAccount);
//                userInfoMap.put(itemMap.get("demention").toString(), itemMap.get("dementionScore"));
//                userInfoMap.put("dementionArray", userInfoMap.get("dementionArray")+","+itemMap.get("demention").toString());
//                userInfoMap.put("scoreArray", userInfoMap.get("scoreArray")+","+itemMap.get("dementionScore").toString());
//                userInfoMap.put("demention" + index, itemMap.get("demention").toString());
//                userInfoMap.put("dementionScore" + index++, itemMap.get("dementionScore"));
//                int itemScore = Integer.parseInt(userInfoMap.get("totalScore").toString()) ;
//                userInfoMap.put("totalScore", +totalScore+itemScore);
//            }else{
//                itemMap.put("dementionArray",itemMap.get("demention").toString());
//                itemMap.put("scoreArray",itemMap.get("dementionScore").toString());
//                itemMap.put("totalScore",totalScore);
//                newMap.put(userAccount, itemMap);
//            }
//        }
        for (Map<String, Object> itemMap : queriedData) {
            String userAccount = itemMap.get("appraiserAccount").toString();
            if (newMap.containsKey(userAccount)) {
                Map<String, Object> userInfoMap = newMap.get(userAccount);
                userInfoMap.put(itemMap.get("demention").toString(), itemMap.get("dementionScore"));
                userInfoMap.put("dementionArray", userInfoMap.get("dementionArray")+","+itemMap.get("demention").toString());
                userInfoMap.put("scoreArray", userInfoMap.get("scoreArray")+","+itemMap.get("dementionScore").toString());
                userInfoMap.put("demention" + index, itemMap.get("demention").toString());
                userInfoMap.put("dementionScore" + index++, itemMap.get("dementionScore"));
            }else{
                itemMap.put("dementionArray",itemMap.get("demention").toString());
                itemMap.put("scoreArray",itemMap.get("dementionScore").toString());
                newMap.put(userAccount, itemMap);
            }
        }
        System.out.println(newMap);
//        Double totalScore = Double.parseDouble("38.0");
//        Integer totalScore1 = Integer.parseInt("38.0");
//        System.out.println(totalScore);
//        System.out.println(totalScore1);


        List<Map<String, String>> list = new ArrayList<>();
        Map<String, String> map1 = new HashMap<>();
        map1.put("key1", "value1");
        map1.put("key-1", "value的值1");
        map1.put("key2", "value2");
        list.add(map1);

        Map<String, String> map2 = new HashMap<>();
        map2.put("key1", "value3");
        map2.put("key2", "value4");
        map1.put("key-2", "value的值2");

        list.add(map2);

        Map<String, String> resultMap = new HashMap<>();
        for (Map<String, String> map : list) {
            for (Map.Entry<String, String> entry : map.entrySet()) {
                if (resultMap.containsKey(entry.getKey())) {
//					resultMap.put(entry.getKey(), resultMap.get(entry.getKey()) + "," + entry.getValue());
                    resultMap.put(entry.getValue(),  entry.getValue());
                } else {
                    resultMap.put(entry.getKey(), entry.getValue());
                }
            }
        }

        System.out.println(resultMap);
//		System.out.println(queriedData);

    }


    public static List<Map<String, Object>> removeRepeatMapByKey(List<Map<String, Object>>list, String mapKey){
//		if (CollectionUtils.isNullOrEmpty(list)) return null;
        if (list.size()==0) return null;

        List<Map<String, Object>> listMap = new ArrayList<>();
        Map<String, Map> msp = new HashMap<>();
        for(int i = list.size()-1 ; i>=0; i--){
            Map map = list.get(i);
            String id =  map.get(mapKey).toString();//错误举例 (String)map.get(mapKey);
            map.remove(mapKey);
            msp.put(id, map);
        }
        Set<String> mspKey = msp.keySet();
        for(String key: mspKey){
            Map newMap = msp.get(key);
            newMap.put(mapKey, key);
            listMap.add(newMap);
        }
        return listMap;
    }

    public static List<Map<String, Object>> removeRepeatMapByKey1(List<Map<String, Object>>list, String mapKey){
//		if (CollectionUtils.isNullOrEmpty(list)) return null;
        if (list.size()==0) return null;

        List<Map<String, Object>> listMap = new ArrayList<>();
        Map<String, Map> msp = new HashMap<>();
        for(int i = list.size()-1 ; i>=0; i--){
            Map map = list.get(i);
            String id =  map.get(mapKey).toString();//错误举例 (String)map.get(mapKey);
//			map.remove(mapKey);
            msp.put(id, map);
        }
        Set<String> mspKey = msp.keySet();
        for(String key: mspKey){
            Map newMap = msp.get(key);
//			newMap.put(mapKey, key);
            listMap.add(newMap);
        }
        return listMap;
    }
}
